package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.personal.adapter.PersonalMyFootPrintListAdapter;
import com.yxq.mdt.app.modules.personal.bean.FootBean;
import com.yxq.mdt.app.modules.personal.bean.RecordUserListBean;
import com.yxq.mdt.app.modules.personal.model.FootModel;
import com.yxq.mdt.app.modules.personal.presenter.FootPresenter;
import com.yxq.mdt.app.modules.personal.presenter.IFootContract;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;

import butterknife.BindView;

/**
 * @author L
 * @time
 * @describe 其他人的足迹
 */
public class MainPersonalOtherFootprintAc extends BaseActivity<FootPresenter> implements IFootContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    PersonalMyFootPrintListAdapter personalMyFootPrintListAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.personal_item_img)
    ImageView personalItemImg;
    @BindView(R.id.item_layout_nike_name)
    TextView itemLayoutNikeName;
    @BindView(R.id.item_number_tv)
    TextView itemNumberTv;
    @BindView(R.id.item_layout_time)
    TextView itemLayoutTime;
    @BindView(R.id.item_layout_province)
    TextView itemLayoutProvince;
    @BindView(R.id.item_layout_attention)
    ImageView itemLayoutAttention;
    @BindView(R.id.user_tv_iv_sex)
    ImageView userTvIvSex;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_other_footprint;
    }

    @Override
    public void initView() {
        setUserInfo();
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.selectMyAccessRecord(1, 20);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.selectMyAccessRecord(page, 20);
        });
        personalMyFootPrintListAdapter = new PersonalMyFootPrintListAdapter(R.layout.item_foot_print_list_layout, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(personalMyFootPrintListAdapter);
        personalMyFootPrintListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mActivity, SocialPersonalHomepageAc.class);
            intent.putExtra("userId", personalMyFootPrintListAdapter.getItem(position).getUserId());
            startActivity(intent);
        });
        personalMyFootPrintListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        personalMyFootPrintListAdapter.isUseEmpty(false);
    }

    private void setUserInfo() {
        RecordUserListBean.ListBean recordUserListBean = JSON.parseObject(getIntent().getStringExtra("data"), RecordUserListBean.ListBean.class);
        if (recordUserListBean != null) {
            GlideHelper.setLoadRoundImage(mActivity, recordUserListBean.getHeadimg(), R.mipmap.head_icon, personalItemImg);
            personalItemImg.setOnClickListener(view -> {
                Intent intent = new Intent(mActivity, SocialPersonalHomepageAc.class);
                intent.putExtra("userId", recordUserListBean.getUserId());
                startActivity(intent);
            });
            itemLayoutAttention.setOnClickListener(view -> {
                Intent intent = new Intent(mActivity, SocialPersonalHomepageAc.class);
                intent.putExtra("userId", recordUserListBean.getUserId());
                startActivity(intent);
            });
            itemLayoutNikeName.setText(recordUserListBean.getGaName());
            mCommonTitleBar.getCenterTextView().setText(recordUserListBean.getGaName() + "的访问记录");
            //性别
            userTvIvSex.setImageResource(recordUserListBean.getSex() == 1 ? R.mipmap.xb_nan : R.mipmap.xb_nv);
            //发布时间
            itemLayoutTime.setText(String.format("访问主页%d次，帖子%d次，商品%d次", recordUserListBean.getUserExtNum(), recordUserListBean.getTopicNum(), recordUserListBean.getGoodsNum()));
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new FootPresenter(this, new FootModel());
        refreshLayout.autoRefresh();
    }


    @Override
    public void onLoadSuccess(FootBean footBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            personalMyFootPrintListAdapter.setNewData(footBean.getList());
        } else {
            if (footBean.getList() != null && footBean.getList().size() != 0) {
                personalMyFootPrintListAdapter.addData(footBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        personalMyFootPrintListAdapter.isUseEmpty(!(personalMyFootPrintListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadFailed(String msg) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadUserListSuccess(RecordUserListBean recordUserListBean) {

    }

    @Override
    public void onLoadUserListFailed(String msg) {

    }
}
